package com.seeTools.next.service.sys.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysTenant;
import com.seeTools.next.common.constant.Constants;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.mapper.sys.SysTenantMapper;
import com.seeTools.next.service.BaseServiceImpl;
import com.seeTools.next.service.sys.ISysTenantService;
import com.seeTools.next.util.PageQueryHelper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 租户信息表 服务实现类
 * </p>
 *
 * @author FallenDust
 * @since 2023-05-11
 */
@CacheConfig(cacheNames = "sysTenantCache")
@Service
public class SysTenantServiceImpl extends BaseServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {

    @Resource
    private PageQueryHelper<SysTenant> pageQueryHelper;

    @Override
    public Page<SysTenant> pageInfo(PageQuery<SysTenant> pageQuery) {
        SysTenant params = pageQuery.getParams();
        // 根据查询参数组装查询wrapper
        QueryWrapper<SysTenant> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysTenant> lambdaQueryWrapper = queryWrapper.lambda();
        if (CharSequenceUtil.isNotEmpty(params.getTenantName())) {
            lambdaQueryWrapper.like(SysTenant::getTenantName, params.getTenantName());
        }
        return pageQueryHelper.pageInfo(this, pageQuery, queryWrapper);
    }

    @Override
    @Cacheable(cacheNames = Constants.CacheKey.TENANT, key = "#tenantId", unless = "#result == null")
    public SysTenant findByTenantIdCache(Long tenantId) {
        return this.getById(tenantId);
    }


    @Override
    @CacheEvict(cacheNames = Constants.CacheKey.TENANT, key = "#entity.id")
    public boolean updateById(SysTenant entity) {
        return super.updateById(entity);
    }

    @Override
    @CacheEvict(cacheNames = Constants.CacheKey.TENANT, key = "#entity.id")
    public boolean removeById(SysTenant entity) {
        return super.removeById(entity);
    }
}
